using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;

namespace DEVOS.Generador
{
    public abstract class Coneccion
    {
        private static String dataSource = ConfigurationManager.AppSettings["DataSource"].ToString();
        private static String tipoSeguridad = ConfigurationManager.AppSettings["TipoSeguridad"].ToString();
        private static String initialCatalog = String.Empty;
        private static SqlConnection conn;

        public static String DataSource
        {
            get { return dataSource; }
        }

        public static String InitialCatalog
        {
            get { return initialCatalog; }
            set
            {
                if (!initialCatalog.Equals(value))
                {
                    if (conn != null && conn.State == ConnectionState.Open)
                    {
                        conn.Close();
                    }
                    conn = null;
                    initialCatalog = value;
                }

            }
        }

        public static SqlConnection Conn
        {
            get
            {
                if (conn == null)
                {
                    String cadenaConeccion =
                        String.Format(
                            "Data Source={0};" +
                            "Initial Catalog={1};" +
                            ((tipoSeguridad=="Integrated")?
                                "Integrated Security=true;":
                                String.Format("User Id={0}; Password={1};",
                                    ConfigurationManager.AppSettings["UserId"].ToString(),
                                    ConfigurationManager.AppSettings["Password"].ToString()
                                )
                            ),
                            dataSource,
                            initialCatalog
                        );
                    conn = new SqlConnection(cadenaConeccion);
                }
                if (conn.State != System.Data.ConnectionState.Open)
                {
                    conn.Open();
                }
                return conn;
            }
            set { conn = value; }
        }

        public static DataTable getDataTable(String cadenaComando)
        {
            SqlConnection conn = Coneccion.Conn;
            SqlCommand comm = new SqlCommand(cadenaComando, conn);

            SqlDataAdapter da = new SqlDataAdapter(comm);

            DataTable dt = new DataTable();
            try
            {
                da.Fill(dt);
            }
            catch (Exception e)
            {
                throw e;
            }

            conn.Close();

            return dt;
        }

        public static DataSet getDataSet(String cadenaComando)
        {
            SqlConnection conn = Coneccion.Conn;
            SqlCommand comm = new SqlCommand(cadenaComando, conn);

            SqlDataAdapter da = new SqlDataAdapter(comm);

            DataSet ds = new DataSet();
            try
            {
                da.Fill(ds);
            }
            catch (Exception e)
            {
                throw e;
            }

            conn.Close();

            return ds;
        }

        public static int ejecutar(String cadenaComando)
        {
            SqlConnection conn = Coneccion.Conn;
            SqlCommand comm = new SqlCommand(cadenaComando, conn);

            int filasAfectadas = (int)comm.ExecuteNonQuery();

            return filasAfectadas;
        }
    }
}
